package com.bill.gaty.mfidelity.activities;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import com.bill.gaty.mfidelity.R;
import com.bill.gaty.mfidelity.beans.Gtf;

import com.bill.gaty.mfidelity.util.GtfConstantes;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class HistoriqueGtfActivity extends Activity {

	private ListView historique_gtf_tab_layout_list;
	private SimpleAdapter adapter;
	private String sauvegarde_edittext_password, sauvegarde_edittext_username;
	private String data;
	private int Offset;
	private int maxresult;
	private String responseMessage;
	public static final int LookUpGtfHistoryActivity_CODE = 0X00BBE;
	private Intent t;
	private TextView textView_next_result;
	private int responseCode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.historique_gtf_tab_layout);
		historique_gtf_tab_layout_list = (ListView) findViewById(R.id.historique_gtf_tab_layout_list);
		textView_next_result = (TextView) findViewById(R.id.textView_next_result);
		maxresult = 20;
		Offset = 0;

		manageView();
		Log.d("maxresult", maxresult + "");
		Log.d("maxresult", Offset + "");

	}

	public void manageView() {
		// TODO Auto-generated method stub
		final SharedPreferences share = getPreferences(MODE_PRIVATE);
		sauvegarde_edittext_password = share.getString(
				"sauvegarde_edittext_password", null);
		sauvegarde_edittext_username = (share.getString(
				"sauvegarde_edittext_username", null));
		Log.i("sauvegarde_edittext_password", sauvegarde_edittext_password + "");
		Log.i("sauvegarde_edittext_username", sauvegarde_edittext_username + "");

		if (null == sauvegarde_edittext_password
				|| null == sauvegarde_edittext_username) {
			Builder adb = new AlertDialog.Builder(HistoriqueGtfActivity.this);
			adb.setTitle(getResources()
					.getString(R.string.label_votre_username)
					+ "/"
					+ getResources().getString(R.string.label_votre_password));
			View vv = getView(R.layout.sauvegarde_credential_layout);
			adb.setView(vv);
			adb.setIcon(R.drawable.historique_home);
			final EditText sauvegarde_edittext_password_edit = (EditText) vv
					.findViewById(R.id.sauvegarde_edittext_password), sauvegarde_edittext_username_edit = (EditText) vv
					.findViewById(R.id.sauvegarde_edittext_username);

			sauvegarde_edittext_password_edit.setText(share.getString(
					"sauvegarde_edittext_password", null));
			sauvegarde_edittext_username_edit.setText(share.getString(
					"sauvegarde_edittext_username", null));

			adb.setNegativeButton(R.string.label_annuler,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							finish();

						}
					});

			adb.setPositiveButton(R.string.label_valider,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							Editor edit = share.edit();
							sauvegarde_edittext_password = sauvegarde_edittext_password_edit
									.getText().toString();
							sauvegarde_edittext_username = sauvegarde_edittext_username_edit
									.getText().toString();
							edit.putString("sauvegarde_edittext_password",
									sauvegarde_edittext_password);
							edit.putString("sauvegarde_edittext_username",
									sauvegarde_edittext_username);
							edit.commit();
							t = new Intent(getBaseContext(),
									LookUpGtfHistoryActivity.class);
							Bundle b = new Bundle();
							b.putString("username",
									sauvegarde_edittext_username);
							b.putString("password",
									sauvegarde_edittext_password);
							b.putInt("Offset", Offset);
							b.putInt("maxresult", maxresult);
							t.putExtras(b);
							startActivityForResult(t,
									LookUpGtfHistoryActivity_CODE);

						}
					});
			adb.show();
		} else {
			t = new Intent(getBaseContext(), LookUpGtfHistoryActivity.class);
			Bundle b = new Bundle();
			b.putString("username", sauvegarde_edittext_username);
			b.putString("password", sauvegarde_edittext_password);
			b.putInt("Offset", Offset);
			b.putInt("maxresult", maxresult);
			t.putExtras(b);
			startActivityForResult(t, LookUpGtfHistoryActivity_CODE);

		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (resultCode == RESULT_OK) {
			if (requestCode == LookUpGtfHistoryActivity_CODE && null != data) {
				Bundle b = data.getExtras();

				this.responseMessage = b.getString("responseMessage");
				this.maxresult = b.getInt("maxresult");
				this.Offset = b.getInt("nextOffset");
				this.data = b.getString("data");
				this.responseCode = b.getInt("responseCode");
				if (responseCode != 200) {
					SharedPreferences share = getPreferences(MODE_PRIVATE);
					Editor edit = share.edit();
					edit.putString("sauvegarde_edittext_password", null);
					edit.putString("sauvegarde_edittext_username", null);
					edit.commit();

					Builder adb = new AlertDialog.Builder(
							HistoriqueGtfActivity.this);
					adb.setTitle(getResources().getString(
							R.string.label_username_password_incorrect));

					adb.setMessage(responseMessage);
					adb.setIcon(android.R.drawable.ic_dialog_info);
					adb.setPositiveButton(
							getResources().getString(
									R.string.label_voulez_vous_continuer),
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									// TODO Auto-generated method stub

									manageView();
								}
							});
					adb.setNegativeButton(
							getResources().getString(R.string.label_annuler),
							null);
					adb.show();

				} else {
					List<Gtf> info = new Gson().fromJson(this.data,
							new TypeToken<List<Gtf>>() {
							}.getType());
					Log.e("List<Gtf> info", info.toString());
					HashMap<String, String> map;
					ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
					SimpleDateFormat format = new SimpleDateFormat(
							GtfConstantes.getDateFormat(Locale.getDefault()
									.getLanguage()), Locale.getDefault());

					for (Gtf gtf : info) {
						Date d = gtf.getSavedate();

						map = new HashMap<String, String>();
						map.put("historique_gtf_list_textView_resultat",
								gtf.getReponse());
						map.put("historique_gtf_list_textView_nom",
								gtf.getNomDeVotreAmi());
						map.put("historique_gtf_list_textView_date",
								format.format(d));
						listItem.add(map);

					}
					if (Offset != 0) {
						textView_next_result.setText(getResources().getString(
								R.string.label_resultat_suivant));

						textView_next_result
								.setOnClickListener(new View.OnClickListener() {

									@Override
									public void onClick(View v) {
										// TODO Auto-generated method stub
										manageView();
									}
								});
					}
					adapter = new SimpleAdapter(
							getBaseContext(),
							listItem,
							R.layout.historique_gtf_layout_listitem_layout,
							new String[] { "historique_gtf_list_textView_nom",
									"historique_gtf_list_textView_date",
									"historique_gtf_list_textView_resultat" },
							new int[] { R.id.historique_gtf_list_textView_nom,
									R.id.historique_gtf_list_textView_date,
									R.id.historique_gtf_list_textView_resultat });

					historique_gtf_tab_layout_list.setAdapter(adapter);
				}
			}
		}

	}

	public View getView(int viewlayout) {
		LayoutInflater ltInflater = getLayoutInflater();
		return ltInflater.inflate(viewlayout, null, false);
	}
}
